/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import com.jiadakeji.ppm.modules.business.entity.Advise;
import com.jiadakeji.ppm.modules.business.service.AdviseService;

/**
 * 投诉建议Controller
 * @author HHAOYU
 * @version 2019-08-21
 */
@Controller
@RequestMapping(value = "${adminPath}/business/advise")
public class AdviseController extends BaseController {

	@Autowired
	private AdviseService adviseService;
	
	@ModelAttribute
	public Advise get(@RequestParam(required=false) String id) {
		Advise entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = adviseService.get(id);
		}
		if (entity == null){
			entity = new Advise();
		}
		return entity;
	}
	
	@RequiresPermissions("business:advise:view")
	@RequestMapping(value = {"list", ""})
	public String list(Advise advise, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Advise> page = adviseService.findPage(new Page<Advise>(request, response), advise); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("advise", advise);
		return "modules/business/adviseList";
	}

	@RequiresPermissions("business:advise:view")
	@RequestMapping(value = "form")
	public String form(Advise advise, Model model) {
		model.addAttribute("advise", advise);
		return "modules/business/adviseForm";
	}

	@RequiresPermissions("business:advise:edit")
	@RequestMapping(value = "save")
	public String save(Advise advise, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, advise)){
			return form(advise, model);
		}
		adviseService.save(advise);
		addMessage(redirectAttributes, "保存投诉建议成功");
		
		redirectAttributes.addFlashAttribute("advise", UserUtils.getCache("advise"));
		return "redirect:"+Global.getAdminPath()+"/business/advise/?repage";
	}
	
	@RequiresPermissions("business:advise:edit")
	@RequestMapping(value = "delete")
	public String delete(Advise advise, RedirectAttributes redirectAttributes) {
		adviseService.delete(advise);
		addMessage(redirectAttributes, "删除投诉建议成功");
		
		redirectAttributes.addFlashAttribute("advise", UserUtils.getCache("advise"));
		return "redirect:"+Global.getAdminPath()+"/business/advise/?repage";
	}

}